package com.ruyuan.commodity.common.mamager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruyuan.commodity.common.mamager.GoodsParamsManager;
import com.ruyuan.commodity.common.mapper.GoodsParamsMapper;
import com.ruyuan.commodity.common.mapper.ParameterGroupMapper;
import com.ruyuan.commodity.common.mapper.ParametersMapper;
import com.ruyuan.commodity.model.goods.dos.GoodsParamsDO;
import com.ruyuan.commodity.model.goods.dos.ParameterGroupDO;
import com.ruyuan.commodity.model.goods.vo.GoodsParamsGroupVO;
import com.ruyuan.commodity.model.goods.vo.GoodsParamsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 商品参数
 */
@Service
public class GoodsParamsManagerImpl implements GoodsParamsManager {

    /**
     * 商品属性的mapper组件
     */
    @Autowired
    private GoodsParamsMapper goodsParamsMapper;
    /**
     * 属性分组的mapper组件
     */
    @Autowired
    private ParameterGroupMapper parameterGroupMapper;
    /**
     * 属性键值对的模板的mapper组件
     */
    @Autowired
    private ParametersMapper parametersMapper;

    /**
     * 添加商品关联的参数
     *
     * @param goodsId   商品id
     * @param paramList 参数集合
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addParams(List<GoodsParamsDO> paramList, Long goodsId) {

        //先删除该商品所有参数
        this.goodsParamsMapper.delete(new QueryWrapper<GoodsParamsDO>()
                .eq("goods_id",goodsId));

        //再添加商品参数
        if (paramList != null) {
            for (GoodsParamsDO param : paramList) {
                param.setGoodsId(goodsId);
                this.goodsParamsMapper.insert(param);
            }
        }
    }

    /**
     * 修改商品查询分类和商品关联的参数
     *
     * @param categoryId 分类id
     * @param goodsId 商品id
     * @return 商品参数集合
     */
    @Override
    public List<GoodsParamsGroupVO> queryGoodsParams(Long categoryId, Long goodsId) {

        //查询参数组
        List<ParameterGroupDO> groupList = parameterGroupMapper.selectList(new QueryWrapper<ParameterGroupDO>()
                .eq("category_id",categoryId));

        //查询商品关联的参数及参数值
        List<GoodsParamsVO> paramList = goodsParamsMapper.queryGoodsParamsValue(categoryId,goodsId);

        //拼装返回值
        return convertParamList(groupList, paramList);
    }

    /**
     * 添加商品查询分类和商品关联的参数
     *
     * @param categoryId 分类id
     * @return 商品参数集合
     */
    @Override
    public List<GoodsParamsGroupVO> queryGoodsParams(Long categoryId) {
        //查询参数组
        List<ParameterGroupDO> groupList = parameterGroupMapper.selectList(new QueryWrapper<ParameterGroupDO>()
                .eq("category_id",categoryId));

        //查询分类关联的参数
        List<GoodsParamsVO> paramList = this.parametersMapper.queryParams(categoryId);

        //拼装返回值
        List<GoodsParamsGroupVO> resList = this.convertParamList(groupList, paramList);

        return resList;
    }

    /**
     * 拼装返回值
     *
     * @param paramList 商品参数集合
     * @return 商品参数集合
     */
    private List<GoodsParamsGroupVO> convertParamList(List<ParameterGroupDO> groupList, List<GoodsParamsVO> paramList) {
        // 根据组id分组
        Map<Long, List<GoodsParamsVO>> map = new HashMap<>(16);
        for (GoodsParamsVO param : paramList) {
            if (map.get(param.getGroupId()) != null) {
                map.get(param.getGroupId()).add(param);
            } else {
                List<GoodsParamsVO> goodsParamsVOList = new ArrayList<>();
                goodsParamsVOList.add(param);
                map.put(param.getGroupId(), goodsParamsVOList);
            }
        }
        // 将参数DO转VO
        List<GoodsParamsGroupVO> resList = new ArrayList<>();
        for (ParameterGroupDO group : groupList) {
            GoodsParamsGroupVO goodsParamsGroupVO = new GoodsParamsGroupVO();
            goodsParamsGroupVO.setGroupName(group.getGroupName());
            goodsParamsGroupVO.setGroupId(group.getGroupId());
            goodsParamsGroupVO.setParams(map.get(group.getGroupId()));
            resList.add(goodsParamsGroupVO);
        }
        return resList;
    }

}
